package org.apache.database.helper;

import org.apache.database.dao.Condition;
import org.apache.database.enums.UpdataActionEnum;
import org.apache.database.utils.AnnotationUtil;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UpdateHelper extends BaseHelper {

    public int updateById(Object o) {
        String tableName = getTabName(o.getClass());

        Field[] declaredFields = o.getClass().getDeclaredFields();
        HashMap<Serializable, Serializable> fieldMapValue = new HashMap<>();

        /**
         * 获取标有主键注解的字段
         */
        Field primaryKeyField = AnnotationUtil.getPrimaryKeyField(o);

        String primaryKey = AnnotationUtil.getColumnName(primaryKeyField);
        try {
            for (Field declaredField : declaredFields) {
                if (declaredField.equals(primaryKeyField)) {
                    continue;
                }
                declaredField.setAccessible(true);
                String columnName = AnnotationUtil.getColumnName(declaredField);
                fieldMapValue.put(columnName, (Serializable) declaredField.get(o));
            }
            Serializable valueByAnnotation = (Serializable) primaryKeyField.get(o);

            String s = UpdataActionEnum.BASE.init().withTableName(tableName)
                    .updateById(primaryKey, valueByAnnotation, fieldMapValue);

            return  getSqlExecute().execute(s);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return -1;
    }

    public int update(Object o, Condition condition, String... field) {

        try {
            String tableName = getTabName(o.getClass());
            Field primaryKeyField = AnnotationUtil.getPrimaryKeyField(o);

            Map<Serializable, Serializable> valueMap = new HashMap<>();
            Field[] declaredFields = o.getClass().getDeclaredFields();
            if (field.length==0){
                for (Field declaredField : declaredFields) {
                    if (declaredField.equals(primaryKeyField)) {
                        continue;
                    }
                    declaredField.setAccessible(true);
                    String columnName = AnnotationUtil.getColumnName(declaredField);
                    valueMap.put(columnName, (Serializable) declaredField.get(o));
                }
            }else {
                List<String> updateField = Arrays.asList(field);

                for (Field declaredField : declaredFields) {
                    declaredField.setAccessible(true);
                    /**
                     * 获取到这个列名
                     */
                    String columnName = AnnotationUtil.getColumnName(declaredField);

                    /**
                     * 如果这个字段需要更新
                     */
                    if (updateField.contains(columnName)) {
                        valueMap.put(columnName, (Serializable) declaredField.get(o));

                    }

                }
            }
            String sql = UpdataActionEnum.BASE.init().withTableName(tableName)
                    .update(condition, valueMap);

            return  getSqlExecute().execute(sql);

        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return -1;

    }

}
